package com.zqweb.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zqweb.gulimall.product.entity.AttrEntity;
import com.zqweb.gulimall.product.service.AttrAttrgroupRelationService;
import com.zqweb.gulimall.product.service.AttrService;
import com.zqweb.gulimall.product.service.CategoryService;
import com.zqweb.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.zqweb.gulimall.product.vo.AttrRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zqweb.gulimall.product.entity.AttrGroupEntity;
import com.zqweb.gulimall.product.service.AttrGroupService;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.R;

/**
 * 属性分组
 *
 * @author zq
 * @email zq@gmail.com
 * @date 2021-11-26 18:38:54
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表（分页查询）
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){
        PageUtils page = attrGroupService.queryPage(params,catId);

        return R.ok().put("page", page);
    }

    /**
     * 查询指定groupId可以新增的属性，并分页展示
     */
    @GetMapping("/{groupId}/noattr/relation")
    public R listNoRelation(@PathVariable("groupId") Long groupId,@RequestParam Map<String, Object> params){
        PageUtils page = attrService.listNoRelation(params,groupId);
        return R.ok().put("page",page);
    }

    /**
     * 查询出指定attrGroupId关联的分组信息
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R getAttrsByGroupId(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> list =  attrGroupService.getAttrsByGroupId(attrGroupId);
        return R.ok().put("data",list);
    }

    /**
     * 查询指定catlogId下所有分组及对应属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupWithAttrsByCatelogId(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrgroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",list);
    }


    /**
     * 给指定attrGroupId新增属性关联
     */
    @PostMapping("/attr/relation")
    public R saveBatchNewAttrAttrGroupRelation(@RequestBody List<AttrRelationVo> relationVos){
        attrAttrgroupRelationService.saveBatchNewAttrAttrGroupRelation(relationVos);
        return R.ok();
    }


    /**
     * 从规格属性与属性分组的关联表中删除关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelations(@RequestBody AttrRelationVo[] relationVos){
        attrService.deleteRelations(relationVos);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 查询到分组信息中的catelogId，进而获取到该catelogId的层级catelogPath，用于前端回显
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.getCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
